package wt.hpad;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import wt.hpad.model.bean.Dish;
import wt.hpad.model.bean.DishDetail;
import wt.hpad.model.bean.DishType;
import wt.hpad.model.bean.ResponseMessage;
import wt.hpad.model.bean.TableInfo;
import wt.hpad.model.local.DBHelper;
import wt.hpad.model.remote.HttpService;
import wt.hpad.model.remote.HttpUtils;
import wt.hpad.utils.GsonUtil;

import static org.junit.Assert.assertEquals;
import static wt.hpad.utils.LogUtil.logI;
import static wt.hpad.utils.LogUtil.logILine;

/**
 * Instrumentation test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    private HttpService mHttpService;
    private String baseurl = "http://10.0.2.2:8080/ct/";

    @Test
    public void useAppContext() throws Exception {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();

        assertEquals("wt.hpad", appContext.getPackageName());
    }


    @Test
    public void test_menu() {   // 获取所有菜信息
        HttpUtils httpUtils = new HttpUtils(baseurl);
        mHttpService = (HttpService) httpUtils.setupRetrofit(HttpService.class);
        Call<ResponseMessage> dishes = mHttpService.getAllDishes();
        dishes.enqueue(new Callback<ResponseMessage>() {
            @Override
            public void onResponse(Call<ResponseMessage> call, Response<ResponseMessage> response) {
                List<Dish> dataList = response.body().dataList;
                logILine("test_menu", "onResponse");
                for (Dish dish : dataList) {
                    logI("test_menu", "onResponse |  ", "" + dish.toString());
                }
                logILine("test_menu", "onResponse");
            }

            @Override
            public void onFailure(Call<ResponseMessage> call, Throwable t) {

            }
        });
    }

    @Test
    public void test_table() { // 获取所有桌台信息pass
        HttpUtils httpUtils = new HttpUtils(baseurl);
        mHttpService = (HttpService) httpUtils.setupRetrofit(HttpService.class);
        Call<ResponseMessage> result = mHttpService.getAllTableInfo();
        result.enqueue(new Callback<ResponseMessage>() {
            @Override
            public void onResponse(Call<ResponseMessage> call, Response<ResponseMessage> response) {
                List<TableInfo> dataList = response.body().dataList;
                logILine("test_table", "onResponse");
                for (TableInfo table : dataList) {
                    logI("test_table", "onResponse |  ", "" + table.toString());
                }
                logILine("test_table", "onResponse");
            }

            @Override
            public void onFailure(Call<ResponseMessage> call, Throwable t) {

            }
        });
    }

    @Test
    public void test_table_one() { // 获取某桌菜品pass
        HttpUtils httpUtils = new HttpUtils(baseurl);
        mHttpService = (HttpService) httpUtils.setupRetrofit(HttpService.class);
        Call<ResponseMessage> result = mHttpService.getTableDishes("A3");
        result.enqueue(new Callback<ResponseMessage>() {
            @Override
            public void onResponse(Call<ResponseMessage> call, Response<ResponseMessage> response) {
                List<DishDetail> dataList = response.body().dataList;
                logILine("test_table", "onResponse");
                if (dataList != null) {
                    for (DishDetail obj : dataList) {
                        logI("test_table", "onResponse |  ", "" + obj.toString());
                    }
                } else {
                    logI("test_table", "onResponse |  ", "" + "未点菜");
                }
                logILine("test_table", "onResponse");
            }

            @Override
            public void onFailure(Call<ResponseMessage> call, Throwable t) {

            }
        });
    }

    @Test
    public void test_login() { // 登录pass
        HttpUtils httpUtils = new HttpUtils(baseurl);
        mHttpService = (HttpService) httpUtils.setupRetrofit(HttpService.class);
        Call<ResponseMessage> result = mHttpService.login("11");
        result.enqueue(new Callback<ResponseMessage>() {
            @Override
            public void onResponse(Call<ResponseMessage> call, Response<ResponseMessage> response) {
                int state = response.body().state;
                logILine("test_table", "onResponse");
                logI("test_table", "onResponse |  ", "login state=" + state);
                logILine("test_table", "onResponse");
            }

            @Override
            public void onFailure(Call<ResponseMessage> call, Throwable t) {

            }
        });
    }

    @Test
    public void test_return() { // 退菜pass
        HttpUtils httpUtils = new HttpUtils(baseurl);
        mHttpService = (HttpService) httpUtils.setupRetrofit(HttpService.class);
        Call<ResponseMessage> result = mHttpService.dishRetreat(479, 1, "点错菜");
        result.enqueue(new Callback<ResponseMessage>() {
            @Override
            public void onResponse(Call<ResponseMessage> call, Response<ResponseMessage> response) {
                int state = response.body().state;
                logILine("test_return", "onResponse");
                logI("test_return", "onResponse |  ", "state=" + state);
                logILine("test_return", "onResponse");
            }

            @Override
            public void onFailure(Call<ResponseMessage> call, Throwable t) {

            }
        });
    }

    @Test
    public void test_order() { // 点菜下单pass
        HttpUtils httpUtils = new HttpUtils(baseurl);
        mHttpService = (HttpService) httpUtils.setupRetrofit(HttpService.class);
        List<DishDetail> dishes = new ArrayList<>();
        DishDetail dish = new DishDetail(0, 1003, "蔬菜沙拉", 130, 1, 0, "不加葱", "");
        dishes.add(dish);
        String jsonString = GsonUtil.toJsonString(dishes);
        logI("test_order", "test_order |  ", "" + jsonString);
        Call<ResponseMessage> result = mHttpService.placeOrder("B2", "", 0 + "", jsonString);
        result.enqueue(new Callback<ResponseMessage>() {
            @Override
            public void onResponse(Call<ResponseMessage> call, Response<ResponseMessage> response) {
                int state = response.body().state;
                logILine("test_order", "onResponse");
                logI("test_order", "onResponse |  ", "state=" + state);
                logILine("test_order", "onResponse");
            }

            @Override
            public void onFailure(Call<ResponseMessage> call, Throwable t) {
                logI("ExampleInstrumentedTest", "onFailure |  ", "" + t.toString());
            }
        });
    }

    @Test
    public void test_type() { // 获取所有菜分类pass
        HttpUtils httpUtils = new HttpUtils(baseurl);
        mHttpService = (HttpService) httpUtils.setupRetrofit(HttpService.class);
        Call<ResponseMessage> result = mHttpService.getDishType();
        result.enqueue(new Callback<ResponseMessage>() {
            @Override
            public void onResponse(Call<ResponseMessage> call, Response<ResponseMessage> response) {
                List<DishType> dataList = response.body().dataList;
                logILine("test_type", "onResponse");
                for (DishType obj : dataList) {
                    logI("test_type", "onResponse |  ", obj.toString());
                }
                logILine("test_type", "onResponse");
            }

            @Override
            public void onFailure(Call<ResponseMessage> call, Throwable t) {
                logI("test_type", "onFailure |  ", "" + t.toString());
            }
        });
    }

    @Test
    public void test_litePal() {
//        List<News> newsList = DataSupport.select("title", "content")
//                .where("commentcount > ?", "0").find(News.class);

        List<Dish> datas = DBHelper.queryDishForType(1);
        for (Dish dish : datas) {
            logI("test_litePal", "test_litePal |  ", "" + dish.dishID);
        }
    }

    @Test
    public void nor01() {
        for (int i = 0; i < 5; i++) {
            try {
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            logI("ExampleInstrumentedTest", "nor01 |  ", "->" + System.currentTimeMillis());
        }

    }


}
